<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>处理申请 - 售后管理系统</title>
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }

    body {
      font-family: 'Microsoft YaHei', Arial, sans-serif;
      background-color: #f5f5f5;
    }

    .header {
      background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
      color: white;
      padding: 20px;
      display: flex;
      justify-content: space-between;
      align-items: center;
    }

    .header h1 {
      font-size: 24px;
    }

    .user-info {
      display: flex;
      align-items: center;
      gap: 15px;
    }

    .logout-btn {
      background: rgba(255, 255, 255, 0.2);
      border: 1px solid rgba(255, 255, 255, 0.3);
      color: white;
      padding: 8px 16px;
      border-radius: 5px;
      cursor: pointer;
      text-decoration: none;
    }

    .container {
      max-width: 1200px;
      margin: 0 auto;
      padding: 20px;
    }

    .content h2 {
      margin-bottom: 20px;
      color: #333;
    }

    .application-card {
      background: white;
      border-radius: 10px;
      padding: 20px;
      box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
      margin-bottom: 20px;
    }

    .application-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 20px;
      padding-bottom: 15px;
      border-bottom: 1px solid #e1e5e9;
    }

    .application-id {
      font-size: 18px;
      font-weight: bold;
      color: #333;
    }

    .status-badge {
      padding: 4px 12px;
      border-radius: 20px;
      font-size: 12px;
      font-weight: 500;
    }

    .status-pending {
      background-color: #fff3cd;
      color: #856404;
    }

    .application-details {
      display: grid;
      grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
      gap: 20px;
      margin-bottom: 25px;
    }

    .detail-item {
      display: flex;
      flex-direction: column;
    }

    .detail-label {
      font-size: 13px;
      color: #666;
      margin-bottom: 5px;
    }

    .detail-value {
      font-size: 15px;
      color: #333;
      font-weight: 500;
    }

    .processing-form {
      background: white;
      border-radius: 10px;
      padding: 20px;
      box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
    }

    .form-group {
      margin-bottom: 20px;
    }

    .form-group label {
      display: block;
      margin-bottom: 8px;
      font-weight: 500;
      color: #333;
    }

    .form-control {
      width: 100%;
      padding: 10px 12px;
      border: 2px solid #e1e5e9;
      border-radius: 5px;
      font-size: 14px;
      resize: vertical;
    }

    textarea.form-control {
      min-height: 120px;
    }

    .action-buttons {
      display: flex;
      gap: 15px;
      margin-top: 30px;
    }

    .action-btn {
      padding: 10px 20px;
      border: 1px solid #667eea;
      background: white;
      color: #667eea;
      border-radius: 5px;
      cursor: pointer;
      font-size: 14px;
      transition: all 0.2s ease;
    }

    .action-btn:hover {
      background: #667eea;
      color: white;
    }

    .action-btn.primary {
      background: #667eea;
      color: white;
    }

    .action-btn.primary:hover {
      background: #5a6fd8;
    }

    .action-btn.reject {
      border-color: #dc3545;
      color: #dc3545;
    }

    .action-btn.reject:hover {
      background: #dc3545;
      color: white;
    }

    .loading {
      text-align: center;
      padding: 60px;
      color: #666;
    }

    .spinner {
      border: 3px solid #f3f3f3;
      border-top: 3px solid #667eea;
      border-radius: 50%;
      width: 40px;
      height: 40px;
      animation: spin 1s linear infinite;
      display: inline-block;
      margin-bottom: 15px;
    }

    @keyframes spin {
      0% { transform: rotate(0deg); }
      100% { transform: rotate(360deg); }
    }

    .back-link {
      display: inline-block;
      margin-bottom: 15px;
      color: #667eea;
      text-decoration: none;
      font-size: 14px;
    }

    .back-link:hover {
      text-decoration: underline;
    }
  </style>
</head>
<body>
<div class="header">
  <h1>售后管理系统 - 客服端</h1>
  <div class="user-info">
    <span>欢迎，<span id="username">客服</span></span>
    <a href="/after_sale_system/page/login" class="logout-btn">退出登录</a>
  </div>
</div>

<div class="container">
  <div class="content">
    <a href="/after_sale_system/page/customerService/pending-audit" class="back-link">← 返回待审核列表</a>
    <h2>处理售后申请</h2>

    <div id="loadingState" class="loading">
      <div class="spinner"></div>
      <p>加载申请信息中...</p>
    </div>

    <div id="applicationContainer" style="display: none;">
      <div class="application-card">
        <div class="application-header">
          <div class="application-id">申请编号: <span id="appId"></span></div>
          <div class="status-badge status-pending" id="appStatus"></div>
        </div>

        <div class="application-details">
          <div class="detail-item">
            <div class="detail-label">申请类型</div>
            <div class="detail-value" id="appType"></div>
          </div>
          <div class="detail-item">
            <div class="detail-label">申请时间</div>
            <div class="detail-value" id="appTime"></div>
          </div>
          <div class="detail-item">
            <div class="detail-label">用户ID</div>
            <div class="detail-value" id="userId"></div>
          </div>
          <div class="detail-item">
            <div class="detail-label">订单号</div>
            <div class="detail-value" id="orderId"></div>
          </div>
          <div class="detail-item">
            <div class="detail-label">商品名称</div>
            <div class="detail-value" id="productName"></div>
          </div>
          <div class="detail-item">
            <div class="detail-label">申请原因</div>
            <div class="detail-value" id="appReason"></div>
          </div>
        </div>
      </div>

      <div class="processing-form">
        <h3>处理信息</h3>
        <div class="form-group">
          <label for="processRemark">处理意见:</label>
          <textarea id="processRemark" class="form-control" placeholder="请输入处理意见..."></textarea>
        </div>

        <div class="action-buttons">
          <button class="action-btn reject" onclick="submitProcess('REJECTED')">拒绝申请</button>
          <button class="action-btn primary" onclick="submitProcess('PROCESSING')">通过并转处理中</button>
        </div>
      </div>
    </div>
  </div>
</div>

<script>
  let applicationId = null;

  // 页面加载时获取申请ID并加载详情
  window.addEventListener('load', function() {
    // 获取URL中的申请ID
    const params = new URLSearchParams(window.location.search);
    applicationId = params.get('id');

    if (!applicationId) {
      alert('无效的申请ID');
      window.location.href = '/after_sale_system/page/customerService/pending-audit';
      return;
    }

    // 获取用户信息
    fetch('/after_sale_system/api/user/info')
            .then(response => response.json())
            .then(data => {
              if (data.success) {
                document.getElementById('username').textContent = data.data;
              }
            })
            .catch(error => {
              console.error('获取用户信息失败:', error);
            });

    // 加载申请详情
    loadApplicationDetail();
  });

  // 加载申请详情
  function loadApplicationDetail() {
    fetch(`/after_sale_system/api/afterSale/detail/${applicationId}`)
            .then(response => response.json())
            .then(data => {
              if (data.success) {
                renderApplicationDetail(data.data);
                document.getElementById('loadingState').style.display = 'none';
                document.getElementById('applicationContainer').style.display = 'block';
              } else {
                alert('获取申请详情失败: ' + data.message);
                document.getElementById('loadingState').innerHTML = '<p>获取申请详情失败，请刷新页面重试</p>';
              }
            })
            .catch(error => {
              console.error('获取申请详情失败:', error);
              document.getElementById('loadingState').innerHTML = '<p>网络错误，请刷新页面重试</p>';
            });
  }

  // 渲染申请详情
  function renderApplicationDetail(app) {
    document.getElementById('appId').textContent = app.id;
    document.getElementById('appStatus').textContent = getStatusText(app.status);
    document.getElementById('appStatus').className = `status-badge status-${getStatusClass(app.status)}`;
    document.getElementById('appType').textContent = getTypeText(app.afterSaleType);
    document.getElementById('appTime').textContent = formatDate(app.applyTime);
    document.getElementById('userId').textContent = app.userId;
    document.getElementById('orderId').textContent = app.orderId || 'N/A';
    document.getElementById('productName').textContent = app.productName || 'N/A';
    document.getElementById('appReason').textContent = app.reason || 'N/A';
  }

  // 获取状态样式类
  function getStatusClass(status) {
    const statusMap = {
      'PENDING_AUDIT': 'pending',
      'PROCESSING': 'processing',
      'COMPLETED': 'completed',
      'REJECTED': 'rejected'
    };
    return statusMap[status] || 'pending';
  }

  // 获取状态文本
  function getStatusText(status) {
    const statusMap = {
      'PENDING_AUDIT': '待审核',
      'PROCESSING': '处理中',
      'COMPLETED': '已完成',
      'REJECTED': '已拒绝'
    };
    return statusMap[status] || status;
  }

  // 获取类型文本
  function getTypeText(type) {
    const typeMap = {
      'RETURN': '退货',
      'EXCHANGE': '换货',
      'REPAIR': '维修',
      'REFUND': '退款'
    };
    return typeMap[type] || type;
  }

  // 格式化日期
  function formatDate(dateString) {
    if (!dateString) return 'N/A';
    const date = new Date(dateString);
    return date.toLocaleString('zh-CN');
  }

  // 提交处理结果
  function submitProcess(status) {
    const remark = document.getElementById('processRemark').value.trim();

    if (!remark) {
      alert('请输入处理意见');
      return;
    }

    if (!confirm(`确定要${status === 'PROCESSING' ? '通过' : '拒绝'}该申请吗？`)) {
      return;
    }

    fetch(`/after_sale_system/api/afterSale/process/${applicationId}`, {
      method: 'PUT',
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
      },
      body: `status=${status}&remark=${encodeURIComponent(remark)}`
    })
            .then(response => response.json())
            .then(data => {
              if (data.success) {
                alert('处理成功');
                window.location.href = '/after_sale_system/page/customerService/pending-audit';
              } else {
                alert('处理失败: ' + data.message);
              }
            })
            .catch(error => {
              console.error('处理申请失败:', error);
              alert('处理失败，请稍后重试');
            });
  }
</script>
</body>
</html>